package cn.edu.neusoft.food229.activity;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import cn.edu.neusoft.food229.R;
import cn.edu.neusoft.food229.bean.Result;
import cn.edu.neusoft.food229.listener.ResultListener;
import cn.edu.neusoft.food229.model.RegisterModel;

public class RegtisterActivity extends AppCompatActivity {
    private EditText etname,etpass,etphone,etaddress,etpass2,etbz;
    private Button bt_register,bt_back;
    private String success;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_regtister);
        initViews();
    }
    private ResultListener resultListener =new ResultListener() {
        @Override
        public void onResponce(Result result) {
            success=result.getSuccess();
            Log.i("RegisterActivity","--------success=="+success);
            if(success.equals("1")){
                Toast.makeText(RegtisterActivity.this, "     注     册    成    功  ", Toast.LENGTH_SHORT).show();

                Intent intent=new Intent();
                intent.putExtra("name",etname.getText().toString());
                intent.putExtra("password",etpass.getText().toString());
                setResult(RESULT_OK,intent);
                finish();
            }
            else{
                Toast.makeText(RegtisterActivity.this, "     ----------注册失败           ", Toast.LENGTH_SHORT).show();
            }

        }

        @Override
        public void onFail(String msg) {
            System.out.println("-----注册失败111："+msg);
            Toast.makeText(RegtisterActivity.this, "注册失败"+msg, Toast.LENGTH_SHORT).show();


        }
    };

    private void initViews() {
        etname=findViewById(R.id.editText7);
        etpass=findViewById(R.id.editText);
        etpass2=findViewById(R.id.editText2);
        etphone=findViewById(R.id.editText9);
        etaddress=findViewById(R.id.editText10);
        etbz=findViewById(R.id.editText5);
        bt_register=findViewById(R.id.button1);
        bt_back=findViewById(R.id.button2);
        bt_register.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                register();
            }
        });
        bt_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent=new Intent(RegtisterActivity.this, LoginActivity.class);
              startActivity(intent);
            }
        });
    }

    private void register() {

        String name=etname.getText().toString();
        String password=etpass.getText().toString();
        String password2=etpass2.getText().toString();
        String phone=etphone.getText().toString();
        String bz=etbz.getText().toString();
        String address=etaddress.getText().toString();
        if(!password.equals(password2))
        {
            Toast.makeText(RegtisterActivity.this,"两次密码不一致 ", Toast.LENGTH_SHORT).show();
            return;
        }
       if(TextUtils.isEmpty(password))
        {
            Toast.makeText(RegtisterActivity.this,"密码不能为空 ", Toast.LENGTH_SHORT).show();
            return;
        }
        if(TextUtils.isEmpty(password2))
        {
            Toast.makeText(RegtisterActivity.this,"密码不能为空 ", Toast.LENGTH_SHORT).show();
            return;
        }
        if(TextUtils.isEmpty(bz))
        {
            Toast.makeText(RegtisterActivity.this,"备注不能为空 ", Toast.LENGTH_SHORT).show();
            return;
        }
        if(TextUtils.isEmpty(name))
        {
            Toast.makeText(RegtisterActivity.this,"姓名不能为空 ", Toast.LENGTH_SHORT).show();
            return;
        }
        if(TextUtils.isEmpty(phone))
        {
            Toast.makeText(RegtisterActivity.this,"联系方式不能为空 ", Toast.LENGTH_SHORT).show();
            return;
        }
        if(TextUtils.isEmpty(address))
        {
            Toast.makeText(RegtisterActivity.this,"地址不能为空 ", Toast.LENGTH_SHORT).show();
            return;
        }
        RegisterModel registerModel=new RegisterModel();
        registerModel.getRegisterResult(name,password,phone,address,bz, resultListener);

    }
}
